// 传入的复制内容
export const copyContent = (data) => {
  return new Promise((resolve, reject) => {
    const oInput = document.createElement("input");
    oInput.setAttribute("readonly", "");
    oInput.value = data;
    document.body.appendChild(oInput);
    // 选择对象
    oInput.select();
    // 选中全部文本
    oInput.setSelectionRange(0, data.length);
    document.execCommand("Copy"); // 执行浏览器复制命令
    oInput.remove();
    resolve();
  });
};

export const formatTime = (timestamp) => {
  let date = new Date(timestamp * 1000);
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return (
    [year, month, day].map(addZero).join("-") +
    " " +
    [hour, minute, second].map(addZero).join(":")
  );
};

export const formatTimeMS = (timestamp) => {
  let date = new Date(timestamp);
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  return [year, month, day].map(addZero).join("-");
};

export const getUrlString = (name) => {
  let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  let r = window.location.search.substr(1).match(reg);
  if (r != null) return decodeURI(r[2]);
  return null;
};

function addZero(v) {
  if (v < 10) {
    return "0" + v;
  } else {
    return v;
  }
}

export const debounce = (func, wait) => {
  let timeout;
  return function() {
    let context = this;
    let args = arguments;
    if (timeout) clearTimeout(timeout);

    timeout = setTimeout(() => {
      func.apply(context, args);
    }, wait);
  };
};

export const smoothScroll = (el, fromTop = 0, defaultStep = 50) => {
  let total = el.offsetTop + fromTop;
  let distance = document.documentElement.scrollTop || document.body.scrollTop;
  // 平滑滚动，时长500ms，每10ms一跳，共50跳
  let step = total / defaultStep;
  if (total > distance) {
    smoothDown();
  } else {
    let newTotal = distance - total;
    step = newTotal / 50;
    smoothUp();
  }

  function smoothDown() {
    if (distance < total) {
      distance += step;
      document.body.scrollTop = distance;
      document.documentElement.scrollTop = distance;
      setTimeout(smoothDown, 10);
    } else {
      document.body.scrollTop = total;
      document.documentElement.scrollTop = total;
    }
  }

  function smoothUp() {
    if (distance > total) {
      distance -= step;
      document.body.scrollTop = distance;
      document.documentElement.scrollTop = distance;
      setTimeout(smoothUp, 10);
    } else {
      document.body.scrollTop = total;
      document.documentElement.scrollTop = total;
    }
  }
};

// 给url添加参数
export const addQuery = (url, name, value) => {
  let mark = "?";
  if (url.indexOf("?") !== -1) {
    mark = "&";
  }
  return url + mark + name + "=" + value;
};

export function gradientColor(startColor, endColor, step) {
  let startRGB = this.colorRgb(startColor); //转换为rgb数组模式
  let startR = startRGB[0];
  let startG = startRGB[1];
  let startB = startRGB[2];

  let endRGB = this.colorRgb(endColor);
  let endR = endRGB[0];
  let endG = endRGB[1];
  let endB = endRGB[2];

  let sR = (endR - startR) / step; //总差值
  let sG = (endG - startG) / step;
  let sB = (endB - startB) / step;

  var colorArr = [];
  for (var i = 0; i < step; i++) {
    //计算每一步的hex值
    var hex = this.colorHex(
      "rgb(" +
        parseInt(sR * i + startR) +
        "," +
        parseInt(sG * i + startG) +
        "," +
        parseInt(sB * i + startB) +
        ")"
    );
    colorArr.push(hex);
  }
  return colorArr;
}

// 将hex表示方式转换为rgb表示方式(这里返回rgb数组模式)
gradientColor.prototype.colorRgb = function(sColor) {
  var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
  var sColor = sColor.toLowerCase();
  if (sColor && reg.test(sColor)) {
    if (sColor.length === 4) {
      var sColorNew = "#";
      for (var i = 1; i < 4; i += 1) {
        sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
      }
      sColor = sColorNew;
    }
    //处理六位的颜色值
    var sColorChange = [];
    for (var i = 1; i < 7; i += 2) {
      sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)));
    }
    return sColorChange;
  } else {
    return sColor;
  }
};

// 将rgb表示方式转换为hex表示方式
gradientColor.prototype.colorHex = function(rgb) {
  var _this = rgb;
  var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
  if (/^(rgb|RGB)/.test(_this)) {
    var aColor = _this.replace(/(?:(|)|rgb|RGB)*/g, "").split(",");
    var strHex = "#";
    for (var i = 0; i < aColor.length; i++) {
      var hex = Number(aColor[i]).toString(16);
      hex = hex < 10 ? 0 + "" + hex : hex; // 保证每个rgb的值为2位
      if (hex === "0") {
        hex += hex;
      }
      strHex += hex;
    }
    if (strHex.length !== 7) {
      strHex = _this;
    }
    return strHex;
  } else if (reg.test(_this)) {
    var aNum = _this.replace(/#/, "").split("");
    if (aNum.length === 6) {
      return _this;
    } else if (aNum.length === 3) {
      var numHex = "#";
      for (var i = 0; i < aNum.length; i += 1) {
        numHex += aNum[i] + aNum[i];
      }
      return numHex;
    }
  } else {
    return _this;
  }
};
